package com.tyqx.hashchainutil.common;

import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainutil.com.example.demo.service.FabricService;
import com.tyqx.hashchainutil.com.example.demo.service.impl.FabricServiceImpl;
import com.tyqx.hashchainutil.org.app.config.Config;
import com.tyqx.hashchainutil.org.app.user.UserContext;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.json.JSONObject;

import java.nio.file.Path;
import java.security.PrivateKey;
import java.util.*;

/*import org.app.chaincode.invocation.InvokeChaincode;
import org.app.client.CAClient;
import org.app.client.ChannelClient;
import org.app.client.FabricClient;
import org.app.config.Config;
import org.app.user.UserContext;
import org.app.util.Util;*/


@Service("chainCode")
public class ChainCode{

    public static HashIndex insertHash(JSONObject insertObj, UserContext userContext) throws Exception {
//        Map<String,Object> hashMap=new HashMap<>();
        HashIndex hashIndex=new HashIndex();
        List<String> keys = new ArrayList<>();
        String insertString=insertObj.toString();
        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();
        byte[] hashBytes=suite.hash(insertString.getBytes());
        PrivateKey privateKey=userContext.getEnrollment().getKey();
        byte[] signBytes=suite.sign(privateKey,hashBytes);
        String hashValue = Base64.getEncoder().encodeToString(hashBytes);
        String hashSignature = Base64.getEncoder().encodeToString(signBytes);
        JSONObject hashObj = new JSONObject();

        String itemIdString="";
        Random random = new Random();
        for (int i = 0; i < 256; i++){
            itemIdString+=String.valueOf(random.nextBoolean() ? 1 : 0);
        }

        long timeStamp=new Date().getTime();

        hashObj.put("docType", "HashItem");
        hashObj.put("itemId", itemIdString);
        hashObj.put("hashValue", hashValue);
        hashObj.put("hashSignature", hashSignature);
        hashObj.put("timeStamp",timeStamp);
        String COMPOSE_PROJECT_NAME="hashchain";

        FabricService fabricService=new FabricServiceImpl();
        hashIndex=fabricService.insert(userContext,hashObj);
        return hashIndex;
    }

    public Map<String,String> insertHash (String userName, JSONObject insertObj, Path clientKeyPath,String methodType,MongoTemplate mongoTemplate) throws Exception{
        String insertString=insertObj.toString();
        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();
        byte[] hashBytes=suite.hash(insertString.getBytes());
        //Base64 Encoded
        String hashValue = Base64.getEncoder().encodeToString(hashBytes);

        String timeStamp=String.valueOf(new Date().getTime());
        Map<String,String> hashIndexMap=new HashMap<>();
        hashIndexMap.put("hashValue",hashValue);
        hashIndexMap.put("timeStamp",timeStamp);
        hashIndexMap.put("txid","111111111111");
        hashIndexMap.put("blockNum","1");
        return hashIndexMap;

    }

    public Map<String,String> insertHash (String userName, JSONObject insertObj, Path clientKeyPath,String methodType) throws Exception{
        String insertString=insertObj.toString();
        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();
        byte[] hashBytes=suite.hash(insertString.getBytes());
        //Base64 Encoded
        String hashValue = Base64.getEncoder().encodeToString(hashBytes);

        String timeStamp=String.valueOf(new Date().getTime());
        Map<String,String> hashIndexMap=new HashMap<>();
        hashIndexMap.put("hashValue",hashValue);
        hashIndexMap.put("timeStamp",timeStamp);
        hashIndexMap.put("txid","111111111111");
        hashIndexMap.put("blockNum","1");
        return hashIndexMap;

    }

    public Map<String,String> insertHashValue (String userName, String hashValue, Path clientKeyPath, String methodType, MongoTemplate mongoTemplate) throws Exception{
        String timeStamp=String.valueOf(new Date().getTime());
        Map<String,String> hashIndexMap=new HashMap<>();
        hashIndexMap.put("hashValue",hashValue);
        hashIndexMap.put("timeStamp",timeStamp);
        hashIndexMap.put("txid","111111111111");
        hashIndexMap.put("blockNum","1");
        return hashIndexMap;

    }

    public Map<String,String> insertHashValue (String userName, String hashValue, Path clientKeyPath, String methodType) throws Exception{
        String timeStamp=String.valueOf(new Date().getTime());
        Map<String,String> hashIndexMap=new HashMap<>();
        hashIndexMap.put("hashValue",hashValue);
        hashIndexMap.put("timeStamp",timeStamp);
        hashIndexMap.put("txid","111111111111");
        hashIndexMap.put("blockNum","1");
        return hashIndexMap;
    }
}
